{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 数字类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#-*- coding: utf-8 -*-\n",
    "#1.1数字类型\n",
    "#int整形 整数 默认输入十进制 另外二进制是0b，八进制是0o，十六进制是0X\n",
    "print (2)\n",
    "print (16==0b10000==0o0==0x10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b10000 0o20 0x10\n"
     ]
    }
   ],
   "source": [
    "#10进制与其他进制的转换,转换后为字符串类型\n",
    "a = bin(16) #转二进制\n",
    "b = oct(16) #转八进制\n",
    "c = hex(16) #转十六机制\n",
    "print (a,b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16 16 16\n"
     ]
    }
   ],
   "source": [
    "#其他进制转十进制\n",
    "d = int(a,2) #二进制转十进制\n",
    "e = int(b,8) #八进制转十进制\n",
    "f = int(c,16) #十六进制转十进制\n",
    "print (d,e,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n",
      "0.30000000000000004\n",
      "0.30000000000000004\n"
     ]
    }
   ],
   "source": [
    "#浮点型 带小数的数 具有不确定性\n",
    "#计算机采用二进制小数来表示浮点数的小数部分，部分小数不能用二进制小数完全表示\n",
    "#通常不会影响计算精度\n",
    "print (2.5)\n",
    "print (0.1+0.2)== 0.3\n",
    "a = 3*0.1\n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "#四舍五入获取精确解\n",
    "a = 0.300000001\n",
    "b = round(a,1)\n",
    "print (b == 0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3+4j)\n",
      "(2+5j)\n"
     ]
    }
   ],
   "source": [
    "#复数类型 a+bj 大写J或者小写j均可\n",
    "print (3+4j)\n",
    "print (2+5J)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2+1j)\n"
     ]
    }
   ],
   "source": [
    "#当虚部系数为1时，需要显式写出\n",
    "print (2+1j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1.0\n"
     ]
    }
   ],
   "source": [
    "#数字运算操作符 加减乘除 + - / *\n",
    "print ((1*3-4*2)/5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "#取反运算 - 乘方运算 ** 整数商 //  模运算 %\n",
    "#整数与浮点数运算当结果是浮点数  除法运算当结果是浮点数\n",
    "#数字运算操作函数\n",
    "#求绝对值\n",
    "print (abs(-5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "32\n"
     ]
    }
   ],
   "source": [
    "#求幂次方\n",
    "print (pow(2,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "#求2的5次幂然后对3求余数\n",
    "print (pow(2,5,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "#四舍五入函数 round第一位为需要四舍五入的数，第二位为保留的小数位数，不写则默认为整数\n",
    "a = 1.618\n",
    "print (round(a))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "1\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "#序列的最大最小值,求和\n",
    "print (max(3,2,1,3,5,2))\n",
    "print (min(3,2,1,3,5,2))\n",
    "print (sum([4,3,2,4,5]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.718281828459045\n",
      "2.0794415416798357\n",
      "2.0\n"
     ]
    }
   ],
   "source": [
    "#借助科学计算库\n",
    "import math\n",
    "print (math.exp(1)) #指数运算 e^x\n",
    "print (math.log(8)) #对数运算 ？\n",
    "print (math.sqrt(4)) #开平方运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n",
      "3.0\n",
      "1.4142135623730951\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = [1,2,3,4,5]\n",
    "print (np.mean(a)) #求均值\n",
    "print (np.median(a)) #求中位数\n",
    "print (np.std(a)) #求标准差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pyhton 124  \n",
      "pyhton &678\n"
     ]
    }
   ],
   "source": [
    "# 1.2字符串类型，由数字 字母 空格或者其它字符组合而成，用 \"\"或'' 表达\n",
    "print (\"pyhton 124  \")\n",
    "print  ('pyhton &678')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i'm 18 year old\n",
      "\"Python\" is good\n"
     ]
    }
   ],
   "source": [
    "#单中有双 双中有单\n",
    "print (\"i'm 18 year old\")\n",
    "print ('\"Python\" is good')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\" Python\" is good\n"
     ]
    }
   ],
   "source": [
    "#双中有双，单中有单 -- 转义符 \\\n",
    "print (\"\\\" Python\\\" is good\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python \n"
     ]
    }
   ],
   "source": [
    "#转义字符可以用来换行\n",
    "s = \"py\\\n",
    "thon \"\n",
    "print (s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "M\n",
      "y\n",
      " \n"
     ]
    }
   ],
   "source": [
    "#字符串的索引\n",
    "#正向索引\n",
    "s = \"My name is Peppa Pig\"\n",
    "print (s[0])\n",
    "print (s[1])\n",
    "print (s[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "g\n",
      "i\n",
      "P\n"
     ]
    }
   ],
   "source": [
    "#反向索引  从-1开始递减\n",
    "print (s[-1])\n",
    "print (s[-2])\n",
    "print (s[-3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pt\n"
     ]
    }
   ],
   "source": [
    "#字符串的切片  变量名[开始位置：结束位置：切片间隔]\n",
    "#切片间隔(位置之差)如不设置默认为1，可省略，切片范围不包括结束位置\n",
    "s = \"Python\"\n",
    "print (s[0:4:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python\n",
      "Python\n",
      "Python\n",
      "Python\n"
     ]
    }
   ],
   "source": [
    "#起始位置是0可以省略，结束位置省略，代表可以取到最后一个字符，可以使用反向索引\n",
    "s = \"Python\"\n",
    "#以下的输出都是python\n",
    "print(s[0:6])\n",
    "print(s[:6])\n",
    "print(s[:])\n",
    "print(s[-6:]) #使用反向索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "987654321\n",
      "987654321\n",
      "987654321\n"
     ]
    }
   ],
   "source": [
    "#反向切片\n",
    "#起始位置是-1可以省略 结束位置省略，代表可以取到第一个字符\n",
    "s = \"123456789\"\n",
    "#以下输出都是987654321\n",
    "print(s[-1:-10:-1])\n",
    "print(s[:-10:-1])\n",
    "print(s[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I love my wife\n"
     ]
    }
   ],
   "source": [
    "#字符串的拼接\n",
    "a = \"I love \"\n",
    "b = \"my wife\"\n",
    "print(a+b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I love my wifeI love my wifeI love my wife\n",
      "I love my wifeI love my wifeI love my wife\n"
     ]
    }
   ],
   "source": [
    "#字符串的成倍复制  字符串*n  n*字符串\n",
    "c = a + b\n",
    "print (c*3)\n",
    "print (3*c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#成员运算\n",
    "#子集in全集  任何一个连续的切片都是原字符串的子集\n",
    "folk_sigers = \"Peter, Paul and Mary\"\n",
    "print (\"Peter\" in folk_sigers)\n",
    "print (\"PPM\" in folk_sigers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P\n",
      "y\n",
      "t\n",
      "h\n",
      "o\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "#遍历字符串字符  for 字符 in 字符串\n",
    "for s in \"Python\":\n",
    "    print (s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "#字符串处理函数\n",
    "#字符串的长度\n",
    "s = \"python\"\n",
    "print (len(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "49\n",
      "97\n",
      "42\n"
     ]
    }
   ],
   "source": [
    "#字符编码\n",
    "#将中文字库，英文字母，数字，特殊字符等编码转化成计算机可识别的二进制数\n",
    "#每个单一字符对应一个唯一的互不重复的二进制编码，python中使用的是Unicode编码\n",
    "#将字符转化为 Unicode码 - ord（字符）\n",
    "print(ord(\"1\"))\n",
    "print(ord(\"a\"))\n",
    "print(ord(\"*\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C', 'C++', 'Java', 'PHP', 'R']\n",
      "Python C C++ Java PHP R\n"
     ]
    }
   ],
   "source": [
    "#将Unicode码转化为字符  chr(Unicode码)\n",
    "# print (chr(1010))\n",
    "# print(chr(10000))\n",
    "# print(chr(12345))\n",
    "# print(chr(23456))\n",
    "#字符串的处理方法\n",
    "#字符串的分割--字符串.split(分割字符)\n",
    "#返回一个列表 原字符串不变\n",
    "languages = \"Python C C++ Java PHP R\"\n",
    "languages_list = languages.split(\" \")\n",
    "print(languages_list)\n",
    "print(languages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,2,3,4,5\n"
     ]
    }
   ],
   "source": [
    "#聚合 \"聚合字符\".join(可迭代数据类型)\n",
    "#可迭代数据类型 ：字符串 字符串列表\n",
    "s = \"12345\"\n",
    "s_join = \",\".join(s) # 将可迭代对象没相邻的两个元素之间加一个逗号\n",
    "print (s_join)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I have many blanks\n",
      "I have many blanks  \n",
      "  I have many blanks\n"
     ]
    }
   ],
   "source": [
    "#删除两端特定字符--字符串.strip(删除字符)\n",
    "#strip从两侧开始搜索，遇到指定字符执行删除，遇到非指定字符，搜索停止\n",
    "#类似的还有左删除lstrip和rstrip\n",
    "s = \"  I have many blanks  \"\n",
    "print (s.strip(\" \"))\n",
    "print (s.lstrip(\" \"))\n",
    "print (s.rstrip(\" \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Py is coming\n"
     ]
    }
   ],
   "source": [
    "#字符串替换 字符串.replace(\"被替换\"，\"替换成\")\n",
    "s = \"Python is coming\"\n",
    "sl = s.replace(\"Python\",\"Py\")\n",
    "print(sl)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "an: 2\n",
      "e: 4\n"
     ]
    }
   ],
   "source": [
    "s = \"Python is an excellent language\"\n",
    "print(\"an:\", s.count(\"an\"))\n",
    "print(\"e:\",s.count(\"e\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PYTHON\n"
     ]
    }
   ],
   "source": [
    "#字符串大小写\n",
    "#字符串.upper() 字母全部大写\n",
    "s = \"Python\"\n",
    "print(s.upper())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python\n"
     ]
    }
   ],
   "source": [
    "#字符串.lower 字母全部小写\n",
    "print (s.lower())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python\n"
     ]
    }
   ],
   "source": [
    "#字符串.title() 首字母大写\n",
    "print (\"python\".title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 布尔类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#1.3 布尔类型 主要用于逻辑运算\n",
    "print (2>1)\n",
    "print (True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#any()只要有一个是非零 就是true\n",
    "#all()所有元素都是非零才是true\n",
    "print (any([False,1,0,None]))\n",
    "print (all([False,1,0,None]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test\n"
     ]
    }
   ],
   "source": [
    "#布尔类型作为指示条件\n",
    "while True:\n",
    "    print (\"test\")\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False  True  True]]\n",
      "[5 7]\n"
     ]
    }
   ],
   "source": [
    "#布尔类型作为掩码\n",
    "x = np.array([[1,3,2,5,7]])\n",
    "print (x>3)\n",
    "print (x[x>3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "#数据类型的判别 type(变量)\n",
    "age = 20\n",
    "name = \"Ada\"\n",
    "print (type(age))\n",
    "print (type(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 预判类型) 承认继承\n",
    "#变量类型是预判类型的字类型，则为真，否则为假\n",
    "print(isinstance(age,int))  # 判断age是否是int这个类的实例\n",
    "print(isinstance(age,object)) #object是所有类的父类\n",
    "print(isinstance(name,object))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#字符串检查方法\n",
    "#字符串.isdigit()检查字符串是否只有数字组成\n",
    "#字符串.isalpha()检查字符是否只有字母组成\n",
    "#字符串.isalnum()检查字符串是否只有字母和数字组成\n",
    "age = \"20\"\n",
    "name = 'Ada'\n",
    "print (age.isdigit())\n",
    "print (name.isdigit())\n",
    "print (\"Ada\".isalpha())\n",
    "print (\"Ada20\".isalnum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My age is 20\n"
     ]
    }
   ],
   "source": [
    "#类型转换\n",
    "#数字类型转字符串 str(数字类型)\n",
    "age = 20\n",
    "print (\"My age is \"+str(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "20.0\n",
      "10.1\n"
     ]
    }
   ],
   "source": [
    "#仅有数字组成的字符串转数字 int(),float(),eval()\n",
    "s1 = \"20\"\n",
    "s2 = \"10.1\"\n",
    "print (int(s1))\n",
    "print (float(s1))\n",
    "print (float(s2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n"
     ]
    }
   ],
   "source": [
    "#用eval可以直接取出字符串里面的内容\n",
    "print (eval(s1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}